import { request, API_BASE_URL } from './api'

// 文件分类相关API
export const getFileCategories = () => {
  const url = `${API_BASE_URL}/file-storage/categories`
  return request(url)
}

export const createFileCategory = (data) => {
  const url = `${API_BASE_URL}/file-storage/categories`
  return request(url, {
    method: 'POST',
    body: JSON.stringify(data)
  })
}

export const updateFileCategory = (id, data) => {
  const url = `${API_BASE_URL}/file-storage/categories/${id}`
  return request(url, {
    method: 'PUT',
    body: JSON.stringify(data)
  })
}

export const deleteFileCategory = (id) => {
  const url = `${API_BASE_URL}/file-storage/categories/${id}`
  return request(url, {
    method: 'DELETE'
  })
}

// 文件相关API
export const getFileList = (params) => {
  const searchParams = new URLSearchParams()

  if (params) {
    Object.keys(params).forEach((key) => {
      if (params[key] !== undefined && params[key] !== null) {
        searchParams.append(key, params[key])
      }
    })
  }

  const url = `${API_BASE_URL}/file-storage/files?${searchParams.toString()}`
  return request(url)
}

export const uploadFile = (formData) => {
  const url = `${API_BASE_URL}/file-storage/upload`
  return request(url, {
    method: 'POST',
    body: formData
    // 不设置 headers，让 request 函数自动处理认证和 Content-Type
  })
}

export const getFileDetail = (id) => {
  const url = `${API_BASE_URL}/file-storage/files/${id}`
  return request(url)
}

export const deleteFile = (id) => {
  const url = `${API_BASE_URL}/file-storage/files/${id}`
  return request(url, {
    method: 'DELETE'
  })
}

export const downloadFile = (id) => {
  const url = `${API_BASE_URL}/file-storage/download/${id}`
  return request(url, {
    method: 'GET'
  })
}

// 上传设置相关API
export const getUploadSettings = () => {
  const url = `${API_BASE_URL}/file-storage/settings`
  return request(url)
}

export const updateUploadSettings = (data) => {
  const url = `${API_BASE_URL}/file-storage/settings`
  return request(url, {
    method: 'PUT',
    body: JSON.stringify(data)
  })
}
